<?php
/**
 *
 *
 * @Author: PanChaoZhi <phpzhi@qq.com>
 * @Created: 2019/11/15 15:29
 * @Updated: 2019/11/15 @Name:
 */

namespace app\admin\model\goods;


use app\common\model\goods\GoodsSpec;
use think\Db;
use think\Exception;

class Goods extends \app\common\model\goods\Goods
{

    // 追加属性
    protected $append = [
        'spec_type_text',
        'deduct_stock_type_text',
        'is_postage_text',
        'is_del_text',
        'is_hot_text',
        'is_best_text',
        'is_new_text',
        'is_benefit_text',
        'is_recommend_text',
        'is_seckill_text',
        'is_bargain_text',
        'status_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getSpecTypeList()
    {
        return ['10' => __('Spec_type 10'), '20' => __('Spec_type 20')];
    }

    public function getDeductStockTypeList()
    {
        return ['10' => __('Deduct_stock_type 10'), '20' => __('Deduct_stock_type 20')];
    }

    public function getIsPostageList()
    {
        return ['0' => __('Is_postage 0'), '1' => __('Is_postage 1')];
    }

    public function getIsDelList()
    {
        return ['0' => __('Is_del 0'), '1' => __('Is_del 1')];
    }

    public function getIsHotList()
    {
        return ['0' => __('Is_hot 0'), '1' => __('Is_hot 1')];
    }

    public function getIsBestList()
    {
        return ['0' => __('Is_best 0'), '1' => __('Is_best 1')];
    }

    public function getIsNewList()
    {
        return ['0' => __('Is_new 0'), '1' => __('Is_new 1')];
    }

    public function getIsBenefitList()
    {
        return ['0' => __('Is_benefit 0'), '1' => __('Is_benefit 1')];
    }

    public function getIsRecommendList()
    {
        return ['0' => __('Is_recommend 0'), '1' => __('Is_recommend 1')];
    }

    public function getIsSeckillList()
    {
        return ['0' => __('Is_seckill 0'), '1' => __('Is_seckill 1')];
    }

    public function getIsBargainList()
    {
        return ['0' => __('Is_bargain 0'), '1' => __('Is_bargain 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getSpecTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
        $list = $this->getSpecTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDeductStockTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deduct_stock_type']) ? $data['deduct_stock_type'] : '');
        $list = $this->getDeductStockTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsPostageTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_postage']) ? $data['is_postage'] : '');
        $list = $this->getIsPostageList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsDelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_del']) ? $data['is_del'] : '');
        $list = $this->getIsDelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_hot']) ? $data['is_hot'] : '');
        $list = $this->getIsHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsBestTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_best']) ? $data['is_best'] : '');
        $list = $this->getIsBestList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsNewTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_new']) ? $data['is_new'] : '');
        $list = $this->getIsNewList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsBenefitTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_benefit']) ? $data['is_benefit'] : '');
        $list = $this->getIsBenefitList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_recommend']) ? $data['is_recommend'] : '');
        $list = $this->getIsRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsSeckillTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_seckill']) ? $data['is_seckill'] : '');
        $list = $this->getIsSeckillList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsBargainTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_bargain']) ? $data['is_bargain'] : '');
        $list = $this->getIsBargainList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 添加商品
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    public function add(array $data)
    {
        Db::startTrans();
        try {
            // 添加商品
            $this->allowField(true)->save($data['row']);
            // 添加商品规格
            $this->addGoodsSpec($data);
            Db::commit();
            return true;
        } catch (Exception $exception) {
            Db::rollback();
        }
        return false;
    }

    /**
     * 编辑商品
     * @param $data
     * @return bool
     */
    public function edit(array $data)
    {
        // 开启事务
        Db::startTrans();
        try {
            // 保存商品
            $this->allowField(true)->save($data['row']);
            // 商品规格
            $this->addGoodsSpec($data, true);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 添加或更新商品规格
     * @param $data
     * @param bool $is_update
     * @throws \Exception
     */
    public function addGoodsSpec(&$data, $is_update = false)
    {
        $model = new GoodsSpec();
        // 如果是更新商品，则先删除所有规格
        $is_update && $model->removeAll($this['id']);
        // 添加规格
        if ($data['row']['spec_type'] == 10) {
            $this->spec()->save($data['spec']);
        } elseif ($data['row']['spec_type'] == 20) {
            // 添加商品与规格关系
            $model->addGoodsSpecRel($this['id'], $data['spec_many']['spec_attr']);
            // 添加商品sku
            $model->addSkuList($this['id'], $data['spec_many']['spec_list']);
        }
    }
}